package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlProcess;
import com.ruoyi.sl.service.ISlModuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlWorkshop;
import com.ruoyi.sl.service.ISlWorkshopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间Controller
 * 
 * @author admin
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/sl/workshop")
public class SlWorkshopController extends BaseController
{
    @Autowired
    private ISlWorkshopService slWorkshopService;

    @Autowired
    private ISlModuleService slModuleService;

    /**
     * 查询车间列表
     */
    @PreAuthorize("@ss.hasPermi('sl:workshop:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlWorkshop slWorkshop)
    {
        startPage();
        List<SlWorkshop> list = slWorkshopService.selectSlWorkshopList(slWorkshop);
        return getDataTable(list);
    }

    /**
     * 根据模块ID查询所有车间
     */
    @GetMapping(value = "/wslist/{moduleId}")
    public AjaxResult getWorkshoplistByModuleId(@PathVariable Long moduleId)
    {
        return success(slWorkshopService.selectSlWorkshopListByModuleId(moduleId));
    }


    /**
     * 导出车间列表
     */
    @PreAuthorize("@ss.hasPermi('sl:workshop:export')")
    @Log(title = "车间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlWorkshop slWorkshop)
    {
        List<SlWorkshop> list = slWorkshopService.selectSlWorkshopList(slWorkshop);
        ExcelUtil<SlWorkshop> util = new ExcelUtil<SlWorkshop>(SlWorkshop.class);
        util.exportExcel(response, list, "车间数据");
    }

    /**
     * 获取车间详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:workshop:query')")
    @GetMapping(value = {"/", "/{workshopId}"})
    public AjaxResult getInfo(@PathVariable(value = "workshopId", required = false) Long workshopId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        if (StringUtils.isNotNull(workshopId))
        {
            SlWorkshop slWorkshop = slWorkshopService.selectSlWorkshopByWorkshopId(workshopId);
            ajax.put(AjaxResult.DATA_TAG, slWorkshop);
        }
        return ajax;
        //return success(slWorkshopService.selectSlWorkshopByWorkshopId(workshopId));
    }

    /**
     * 新增车间
     */
    @PreAuthorize("@ss.hasPermi('sl:workshop:add')")
    @Log(title = "车间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlWorkshop slWorkshop)
    {
        if (!slWorkshopService.checkWorkshopName(slWorkshop))
        {
            return error("新增失败,车间名'" + slWorkshop.getWorkshopName() + "'已存在");
        }
        slWorkshop.setCreateBy(getUsername());
        return toAjax(slWorkshopService.insertSlWorkshop(slWorkshop));
    }

    /**
     * 修改车间
     */
    @PreAuthorize("@ss.hasPermi('sl:workshop:edit')")
    @Log(title = "车间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlWorkshop slWorkshop)
    {
        if (!slWorkshopService.checkWorkshopName(slWorkshop))
        {
            return error("修改失败,车间名'" + slWorkshop.getWorkshopName() + "'已存在");
        }
        slWorkshop.setUpdateBy(getUsername());
        return toAjax(slWorkshopService.updateSlWorkshop(slWorkshop));
    }

    /**
     * 删除车间
     */
    @PreAuthorize("@ss.hasPermi('sl:workshop:remove')")
    @Log(title = "车间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workshopIds}")
    public AjaxResult remove(@PathVariable Long[] workshopIds)
    {
        for (int i = 0; i < workshopIds.length; i++) {
            if(!slWorkshopService.checkWorkshopMemberNum(workshopIds[0])){
                return error("该车间有大工序存在，删除失败。");
            }
        }
        return toAjax(slWorkshopService.deleteSlWorkshopByWorkshopIds(workshopIds));
    }
}
